package com.jiang.booksystem.core;

import com.jiang.booksystem.exception.ServiceException;
import com.jiang.booksystem.exception.ValidateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(ValidateException.class)
    @ResponseBody
    public ApiResponse<Void> handleValidateException(ValidateException e) {
        return new ApiResponse<>(-1, e.getMessage(), null);
    }

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ApiResponse<Void> handleServiceException(ServiceException e) {
        logger.warn(e.getMessage(), e);
        return new ApiResponse<>(-1, e.getMessage(), null);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResponse<Void> handleServiceException(Exception e) {
        logger.error(e.getMessage(), e);
        return new ApiResponse<>(-1, e.getMessage(), null);
    }


}
